/*
 * Copyright 2013-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.ssm.controller;

import cn.ssm.pojo.User;
import cn.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.SocketUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Map;

/**
 * @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
 */
@Controller
public class LoginController {

    private static final Long SESSION_TIMEOUT = 60 * 1000L;

    @Autowired
    private UserService userService;

    @PostMapping("/login")
public String login(HttpServletRequest request, String inputCaptcha, @RequestParam("username") String username, @RequestParam("password") String password, Map<String, Object> map) {
        User login = userService.login(username, password);

        if (!StringUtils.hasLength(inputCaptcha)){
            map.put("msg", "验证码不能为空！");
            return "/main/login";
        }
        if (login!=null){
            // 将当前登录的用户名存储到session中
            request.getSession().setAttribute("loginUser", username);
            //从Session中获取验证码
            String saveCaptcha = (String)  request.getServletContext().getAttribute("captchaCode");
            Date saveDate = (Date) request.getServletContext().getAttribute("captchaDate");

            System.out.println(saveCaptcha+"-----------------"+inputCaptcha);
            //比对验证码
            if (inputCaptcha.equals(saveCaptcha)) {
                // 比对日期
                 if (saveDate == null || System.currentTimeMillis() - saveDate.getTime() < SESSION_TIMEOUT) {
                     return "redirect:/main.html";
                 }else {
                     map.put("msg", "验证码过期，请重新输入！");
                     return "/main/login";
                 }
                // }
            }else {
                map.put("msg", "验证码错误！");
                return "/main/login";
            }
            // 防止表单重复提交，通过重定向到主页, 需要添加一个视图
        }
        map.put("msg", "用户名或密码错误！");
        return "/main/login";
    }
    /**
     * 退出登录
     * @return
     */
    @GetMapping("/logout")
    public String logout(HttpSession session) {
        //1. 清空session中的用户信息
        session.removeAttribute("loginUser");
        //2. 再将session进行注销
        session.invalidate();
        //3. 返回登录页面
        return "redirect:/index.html";
    }

}

